package com.rear.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rear.entity.TestContentEntity;
import com.rear.service.TestContentService;
import com.rear.utils.ImgBase64;
import com.rear.utils.PageUtils;
import com.rear.utils.R;


/**
 * 
 * 
 * @author October
 * @email chenzeming@9an-data.com
 * @date 2017-04-07 09:51:38
 */
@Controller
@RequestMapping("testcontent")
public class TestContentController {
	@Autowired
	private TestContentService testContentService;
	
	@RequestMapping("/testcontent.html")
	public String list(){
		return "testcontent/testcontent.html";
	}
	
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	@RequiresPermissions("testcontent:list")
	public R list(Integer page, Integer limit){
		Map<String, Object> map = new HashMap<>();
		map.put("offset", (page - 1) * limit);
		map.put("limit", limit);
		
		//查询列表数据
		List<TestContentEntity> testContentList = testContentService.queryList(map);
		int total = testContentService.queryTotal(map);
		
		PageUtils pageUtil = new PageUtils(testContentList, total, limit, page);
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@ResponseBody
	@RequestMapping("/info/{id}")
	@RequiresPermissions("testcontent:info")
	public R info(@PathVariable("id") Integer id){
		TestContentEntity testContent = testContentService.queryObject(id);
		return R.ok().put("testContent", testContent);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping("/save")
	@RequiresPermissions("testcontent:save")
	public R save(@RequestBody TestContentEntity testContent){
		try {
			//图片base64存储到服务器并返回路径
			String url=ImgBase64.GenerateImage(testContent.getImgBase64());
			testContent.setPicurl(url);
			testContentService.save(testContent);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("testcontent:update")
	public R update(@RequestBody TestContentEntity testContent){
		//图片base64存储到服务器并返回路径
		if (testContent.getImgBase64()!=null&&testContent.getImgBase64()!="") {
			String url=ImgBase64.GenerateImage(testContent.getImgBase64());
			testContent.setPicurl(url);
		}
		testContentService.update(testContent);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("testcontent:delete")
	public R delete(@RequestBody Integer[] ids){
		testContentService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
